home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / getty_ps.000 / getty_ps / getty_ps-2.0.7i / defaults.c < prev    next >
C/C++ Source or Header  |  1994-05-16  |  5KB  |  223 lines

  1. /*
  2. **    $Id: defaults.c,v 2.0 90/09/19 19:42:09 paul Rel $
  3. **
  4. **    Routines to access runtime defaults file.
  5. **    This is to allow program features to be configured
  6. **    without the need to recompile.
  7. **
  8. **    XENIX has defopen(S) and defread(S), but I think this is better,
  9. **    since it reads the file only once, storing the values in core.
  10. **    It is certainly more portable.
  11. */
  12.  
  13. /*
  14. **    Copyright 1989,1990 by Paul Sutcliffe Jr.
  15. **
  16. **    Permission is hereby granted to copy, reproduce, redistribute,
  17. **    or otherwise use this software as long as: there is no monetary
  18. **    profit gained specifically from the use or reproduction or this
  19. **    software, it is not sold, rented, traded or otherwise marketed,
  20. **    and this copyright notice is included prominently in any copy
  21. **    made.
  22. **
  23. **    The author make no claims as to the fitness or correctness of
  24. **    this software for any use whatsoever, and it is provided as is. 
  25. **    Any use of this software is at the user's own risk.
  26. */
  27.  
  28. /*
  29. **    $Log:    defaults.c,v $
  30. **    Revision 2.0  90/09/19  19:42:09  paul
  31. **    Initial 2.0 release
  32. **    
  33. */
  34.  
  35.  
  36. #include "getty.h"
  37. #include "defaults.h"
  38. #include <sys/stat.h>
  39. #include <errno.h>
  40. #include <ctype.h>
  41.  
  42.  
  43. /*
  44. **    defbuild() - create in-core list of defaults
  45. **
  46. **    Returns (DEF**)NULL if no defaults file found or an error occurs.
  47. */
  48.  
  49. DEF **
  50. defbuild(filename)
  51. char *filename;
  52. {
  53.     register int i;
  54.     register DEF *dp;
  55.     register DEF *next;
  56.     FILE *fp;
  57.     char *fname, defname[MAXLINE+1], buf[MAXLINE+1];
  58.     static DEF *deflist[MAXDEF+1];        /* in-core list */
  59.     struct stat st;
  60.     extern int errno;
  61.  
  62.     debug(D_DEF, "defbuild(%s) called",
  63.             ((filename == (char *) NULL) ? "NULL" : filename));
  64.  
  65.     /* look to see if there's a DEFAULTS/MyName.Device file
  66.      */
  67.     (void) sprintf(buf, "%s", DEFAULTS);
  68.     (void) strcat(buf, ".%s");
  69.     (void) sprintf(defname, buf, MyName, Device);
  70.     debug(D_DEF, "looking for %s", defname);
  71.     if ((stat(defname, &st) == FAIL) && errno == ENOENT) {    /* Nope */
  72.         debug(D_DEF, "stat failed, no file");
  73.         (void) sprintf(defname, DEFAULTS, MyName);
  74.     }
  75.  
  76.     fname = (filename != (char *) NULL) ? filename : defname;
  77.  
  78.     /* if fname doesn't begin with a '/', assume it's a
  79.      * filename to be made "DEFAULTS/fname"
  80.      */
  81.     if (*fname != '/') {
  82.         (void) sprintf(defname, DEFAULTS, fname);
  83.         fname = defname;
  84.     }
  85.  
  86.     debug(D_DEF, "fname = (%s)", fname);
  87.  
  88.     if ((fp = defopen(fname)) == (FILE *) NULL) {
  89.         debug(D_DEF, "defopen() failed");
  90.         return((DEF **) NULL);        /* couldn't open file */
  91.     }
  92.  
  93.     for (i=0; i < MAXDEF; i++) {
  94.         if ((dp = defread(fp)) == (DEF *) NULL)
  95.             break;
  96.         if ((next = (DEF *) malloc((unsigned) sizeof(DEF))) ==
  97.             (DEF *) NULL) {
  98.             logerr("malloc() failed: defaults list truncated");
  99.             break;
  100.         }
  101.         next->name = dp->name;
  102.         next->value = dp->value;
  103.         deflist[i] = next;
  104.         debug(D_DEF, "deflist[%d]: name=(%s), value=(%s)",
  105.                 i, deflist[i]->name, deflist[i]->value);
  106.     }
  107.     deflist[i] = (DEF *) NULL;    /* terminate list */
  108.     (void) defclose(fp);
  109.     debug(D_DEF, "defbuild() successful");
  110.     return(deflist);
  111. }
  112.  
  113.  
  114. /*
  115. **    defvalue() - locate the value in "deflist" that matches "name"
  116. **
  117. **    Returns (char*)NULL if no match is made.
  118. */
  119.  
  120. char *
  121. defvalue(deflist, name)
  122. register DEF **deflist;
  123. register char *name;
  124. {
  125.     debug(D_DEF, "defvalue(%s) called", name);
  126.  
  127.     if (deflist != (DEF **) NULL)
  128.         for (; *deflist != (DEF *) NULL; deflist++)
  129.             if (strequal(name, (*deflist)->name)) {
  130.                 debug(D_DEF, "defvalue returns (%s)",
  131.                         (*deflist)->value);
  132.                 return((*deflist)->value);  /* normal exit */
  133.             }
  134.  
  135.     debug(D_DEF, "defvalue returns NULL");
  136.     return((char *) NULL);
  137. }
  138.  
  139.  
  140. /*
  141. **    defopen() - open the defaults file
  142. **
  143. **    Returns (FILE*)NULL if file not found or an error occurs.
  144. */
  145.  
  146. FILE *
  147. defopen(filename)
  148. register char *filename;
  149. {
  150.     if (filename != (char *) NULL)
  151.         return(fopen(filename, "r"));
  152.  
  153.     return((FILE *) NULL);
  154. }
  155.  
  156.  
  157. /*
  158. **    defread() - read a line from the defaults file
  159. **
  160. **    Returns (DEF*)NULL if an error occurs.
  161. */
  162.  
  163. DEF *
  164. defread(fp)
  165. register FILE *fp;
  166. {
  167.     register char *p, *p2;
  168.     char buf[MAXLINE+1];    /* buffer large enough for 1 line */
  169.     static DEF def;
  170.  
  171.     do {
  172.         if (fgets(buf, sizeof(buf), fp) == (char *) NULL)
  173.             return((DEF *) NULL);    /* no more lines */
  174.  
  175.     } while ((buf[0] == '#') || (buf[0] == '\n'));
  176.       /* SMR - ignore comment lines */
  177.  
  178.     buf[strlen(buf)-1] = '\0';        /* rm trailing \n */
  179.  
  180.     /* lines should be in the form "NAME=value"
  181.      */
  182.     if ((p = index(buf, '=')) == (char *) NULL) {
  183.         logerr("bad defaults line: %s", buf);
  184.         return((DEF *) NULL);
  185.     }
  186.     *p++ = '\0';        /* split into two fields, name and value */
  187.     while (*p && isspace(*p)) {
  188.         p++;        /* Jump past space before value. */
  189.     }
  190.     p2 = p + strlen(p) - 1;
  191.     while (isspace(*p2)) {
  192.         *p2-- = 0;    /* Remove spaces from end of value. */
  193.     }
  194.     def.value = strdup(p);
  195.     p = buf;
  196.     while (*p == ' ') {
  197.         p++;        /* Jump past spaces before variable name. */
  198.     }
  199.     p2 = p + strlen(p) - 1;
  200.     while (isspace(*p2)) {
  201.         *p2-- = 0;    /* Remove spaces from end of variable name. */
  202.     }
  203.     def.name = strdup(p);
  204.  
  205.     return(&def);
  206. }
  207.  
  208.  
  209. /*
  210. **    defclose() - closes the defaults file
  211. **
  212. **    Returns EOF if an error occurs.
  213. */
  214.  
  215. int
  216. defclose(fp)
  217. register FILE *fp;
  218. {
  219.     return(fclose(fp));
  220. }
  221.  
  222.  
  223.